package com.campus.campusdelivery.util;

import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.atomic.AtomicInteger;

@Component
public class OrderNumberGenerator {
    
    private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
    private static final AtomicInteger SEQUENCE = new AtomicInteger(1);
    
    public String generateOrderNumber() {
        LocalDateTime now = LocalDateTime.now();
        String timestamp = now.format(FORMATTER);
        int sequence = SEQUENCE.getAndIncrement();
        
        // 如果序列号超过9999，重置为1
        if (sequence > 9999) {
            SEQUENCE.set(1);
            sequence = 1;
        }
        
        return String.format("CD%s%04d", timestamp, sequence);
    }
}
